/*
 * Copyright 2007-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.paoding.rose.web.impl.thread;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.paoding.rose.Rc;
import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.RequestPath;
import net.paoding.rose.web.impl.module.Module;
import net.paoding.rose.web.var.Flash;
import net.paoding.rose.web.var.FlashImpl;
import net.paoding.rose.web.var.Model;
import net.paoding.rose.web.var.ModelImpl;
import net.paoding.rose.web.var.PrivateVar;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;

/**
 * @author zhiliang.wang [qieqie.wang@paoding.net]
 */
public final class InvocationBean implements Invocation {

    public static final Object[] UN_INITIATED_ARRAY = new Object[0];

    private static final Log logger = LogFactory.getLog(InvocationBean.class);

    private Object[] methodParameters = UN_INITIATED_ARRAY; // 在还没有设置方法参数进来时为UN_INITIATED_ARRAY

    private Map<String, Object> attributes;

    private HttpServletRequest request;

    private HttpServletResponse response;

    private RequestPath requestPath;

    private transient Model model;

    private transient Flash flash;

    private MatchResult<ModuleEngine> moduleMatchResult;

    private MatchResult<ControllerEngine> controllerMatchResult;

    private MatchResult<ActionEngine> actionMatchResult;

    private Map<String, Object> attributesSnapshot;

    private boolean multiPartRequest;

    private int executedInterceptorIndex = -1;

    public InvocationBean() {
    }

    protected boolean isMethodParametersInitiated() {
        return methodParameters != UN_INITIATED_ARRAY;
    }

    @Override
    public WebApplicationContext getApplicationContext() {
        return Rc.getWebApplicationContext(getModule().getPath());
    }

    public void setMethodParameters(Object[] methodParameters) {
        this.methodParameters = methodParameters;
    }

    @Override
    public Object getController() {
        return getControllerEngine().getController();
    }

    @Override
    public Class<?> getControllerClass() {
        return getControllerEngine().getControllerClass();
    }

    @Override
    public Method getMethod() {
        return getActionEngine().getMethod();
    }

    @Override
    public String[] getMethodParameterNames() {
        String[] copy = new String[getActionEngine().getParameterNames().length];
        System.arraycopy(getActionEngine().getParameterNames(), 0, copy, 0, copy.length);
        return copy;
    }

    @Override
    public Object[] getMethodParameters() {
        Object[] copy = new Object[methodParameters.length];
        System.arraycopy(methodParameters, 0, copy, 0, copy.length);
        return copy;
    }

    Object[] unsafedGetMethodParameters() {
        return methodParameters;
    }

    @Override
    public Object getMethodParameter(String name) {
        if (!isMethodParametersInitiated()) {
            throw new IllegalStateException();
        }
        String[] names = getActionEngine().getParameterNames();
        for (int i = 0; i < names.length; i++) {
            if (name != null && name.equals(names[i])) {
                return methodParameters[i];
            }
        }
        return null;
    }

    @Override
    public Object getParameter(String name) {
        Object value = null;
        if (isMethodParametersInitiated()) {
            value = getMethodParameter(name);
        }
        if (value == null) {
            value = actionMatchResult.getParameter(name);
        }
        if (value == null) {
            value = controllerMatchResult.getParameter(name);
        }
        if (value == null) {
            value = request.getParameter(name);
        }
        return value;
    }

    @Override
    public void changeMethodParameter(int index, Object value) {
        if (!isMethodParametersInitiated()) {
            throw new IllegalStateException();
        }
        if (value != this.methodParameters[index]) {
            if (logger.isDebugEnabled()) {
                logger.debug("change method parameter "
                        + this.getActionEngine().getParameterNames()[index] + " (index=" + index
                        + ") from '" + this.methodParameters[index] + "' to '" + value + "'");
            }
            Object oldValue = this.methodParameters[index];
            this.methodParameters[index] = value;
            if (logger.isDebugEnabled()) {
                logger.debug("change method parameter at " + index//
                        + ": " + oldValue + "->" + value);
            }
        }
    }

    public void changeMethodParameter(String name, Object value) {
        if (!isMethodParametersInitiated()) {
            throw new IllegalStateException();
        }
        if (StringUtils.isEmpty(name)) {
            throw new NullPointerException("parameter name");
        }
        String[] names = this.getActionEngine().getParameterNames();
        for (int i = 0; i < names.length; i++) {
            if (name.equals(names[i])) {
                changeMethodParameter(i, value);
                return;
            }
        }
    }

    @Override
    public synchronized Object getAttribute(String name) {
        if (attributes == null) {
            return null;
        }
        return attributes.get(name);
    }

    @Override
    public synchronized void removeAttribute(String name) {
        if (attributes != null) {
            attributes.remove(name);
        }
    }

    @Override
    public synchronized Set<String> getAttributeNames() {
        if (attributes == null) {
            return Collections.emptySet();
        }
        return attributes.keySet();
    }

    @Override
    public synchronized Invocation setAttribute(String name, Object value) {
        if (attributes == null) {
            attributes = new HashMap<String, Object>();
        }
        attributes.put(name, value);
        return this;
    }

    @Override
    public Model getModel() {
        if (this.model != null) {
            return this.model;
        }
        Model model = (Model) getRequest().getAttribute("$$paoding-rose.model");
        if (model == null) {
            model = new ModelImpl(this);
            getRequest().setAttribute("$$paoding-rose.model", model);
        }
        return this.model = model;
    }

    @Override
    public Flash getFlash() {
        return getFlash(true);
    }

    public Flash getFlash(boolean create) {
        if (this.flash != null) {
            return this.flash;
        }
        Flash flash = (Flash) getRequest().getAttribute("$$paoding-rose.flash");
        if (flash == null && create) {
            flash = new FlashImpl(this);
            getRequest().setAttribute("$$paoding-rose.flash", flash);
        }
        return this.flash = flash;
    }

    @Override
    public RequestPath getRequestPath() {
        return requestPath;
    }

    @Override
    public HttpServletRequest getRequest() {
        return request;
    }

    @Override
    public HttpServletResponse getResponse() {
        return response;
    }

    public ControllerEngine getControllerEngine() {
        return getActionEngine().getControllerEngine();
    }

    public ActionEngine getActionEngine() {
        if (actionMatchResult == null) {
            throw new NullPointerException("actionMatchResult");
        }
        return actionMatchResult.getMapping().getTarget();
    }

    public MatchResult<ModuleEngine> getModuleMatchResult() {
        return moduleMatchResult;
    }

    public void setModuleMatchResult(MatchResult<ModuleEngine> moduleMatchResult) {
        this.moduleMatchResult = moduleMatchResult;
    }

    public MatchResult<?> getControllerMatchResult() {
        return controllerMatchResult;
    }

    public void setControllerMatchResult(MatchResult<ControllerEngine> controllerMatchResult) {
        this.controllerMatchResult = controllerMatchResult;
    }

    public MatchResult<?> getActionMatchResult() {
        return actionMatchResult;
    }

    public void setActionMatchResult(MatchResult<ActionEngine> actionMatchResult) {
        this.actionMatchResult = actionMatchResult;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
        PrivateVar.setInvocation(this);
    }

    @Override
    public ServletContext getServletContext() {
        return PrivateVar.servletContext();
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    public void setRequestPath(RequestPath requestPath) {
        this.requestPath = requestPath;
    }

    public Module getModule() {
        return this.moduleMatchResult.getMapping().getTarget().getModule();
    }

    public Map<String, Object> getAttributesSnapshot() {
        return attributesSnapshot;
    }

    public void setAttributesSnapshot(Map<String, Object> attributesSnapshot) {
        this.attributesSnapshot = attributesSnapshot;
    }

    public void setMultiPartRequest(boolean multiPartRequest) {
        this.multiPartRequest = multiPartRequest;
    }

    public boolean isMultiPartRequest() {
        return multiPartRequest;
    }

    public void setExecutedInterceptorIndex(int executedInterceptorIndex) {
        this.executedInterceptorIndex = executedInterceptorIndex;
    }

    public int getExecutedInterceptorIndex() {
        return executedInterceptorIndex;
    }

}
